<template>
	<view>
		<!-- 页首 -->
		<zw-header></zw-header>
		<menu-wrapper active-index="2-1" style="background-color: #fff;padding: 0;">
			<view style="display: flex;justify-content:space-between;align-items: center;margin-top: 25px;">
				<view class="lable">我的贴子</view>

			</view>
			<view class="card-body">
				<view class="container">
					<view v-for="(item,index) in items">
						<view style="display: flex;justify-content: flex-start;align-items: center;">
							<view style="display: flex;justify-content: center;align-items: center;">
								<fui-avatar background="#fff" width="90" :src="loginUser.avator"></fui-avatar>
							</view>
							<view class="user-info">
								<view class="user-name elip" style="font-size: 18px;font-weight: 500;">
									{{loginUser.name}}
								</view>
							</view>
						</view>
						<view style="margin-left: 80rpx;">
							<view style="padding: 40rpx 0rpx 0rpx 50rpx;">
								<fui-textarea :radius="20" textareaBorder
									placeholderStyle="color: #A5A5A5;font-size: 16px;" border-color="#F0F0F0"
									background-color="#F0F0F0" v-model="item.content" color="#6D6D6D"
									placeholder="分享你的AI调教经验和趣事" isCounter>
									<view slot="right-bottom"
										style="display: flex;justify-content: flex-end;align-items: center;color: #787878;font-size: 24rpx;font-weight: 500;">
										{{item.content.length}}/800
										<view style="margin-left: 10rpx;">|</view>
										<view @click="clear(index)"
											style="padding: 8rpx 20rpx 8rpx 20rpx; color: #000000;cursor: pointer;">
											清空</view>
									</view>
									</fui-textarea>
							</view>
							<view style="padding: 40rpx 50rpx 0rpx 50rpx;display: flex;justify-content: flex-start;align-items: center;">
								<view class="fui-upload__item" v-for="(item,key) in item.imagelist" style="margin-right: 10rpx;">
									<image :src="item" style="width: 100%;height: 100%;border-radius: 28rpx;"></image>
								</view>
								<view class="fui-upload__item" v-if="item.imagelist.length<=9" @tap.stop="showImg=true;tabIndex=index">
									<fui-icon name="plus"></fui-icon>
								</view>
							</view>
						</view>
					</view>
					<view style="margin-top: 80rpx;">
						<view style="display: flex;justify-content: flex-start;align-items: center;">
							<view style="display: flex;justify-content: center;align-items: center;">
								<fui-avatar background="#fff" width="90" :src="loginUser.avator"></fui-avatar>
							</view>
							<view class="user-info" @click="addItem()" style="cursor: pointer; border: 1px solid #CECECE;border-radius: 100px;padding:12rpx; 32rpx;width: 280rpx;text-align: center;">
								<view class="user-name elip" style="font-size: 30rpx;color: #0033FF;">
									添加另一段帖子
								</view>
							</view>
						</view>
					</view>
					
				</view>
				<view>
					<view style="display: flex;justify-content: center;align-items: center;margin-left: 80rpx;margin-top: 200rpx;">
						<view class="publish" @click="clickOption(3)" style="width: 30%;">
							<view>
								鞋样设计
							</view>
							<fui-icon name="arrowdown" color="#000" size="48" style="margin-left: 20rpx;"></fui-icon>
						</view>
						<view style="width: 70%;">
							<view style="padding-left: 10rpx; border: 1px solid #C4BFBF;border-radius: 20rpx;width: 100%;height: 80rpx;display: flex;justify-content: flex-start;align-items: center;">
								<fui-tag background="#F0F0F0" text="#话题"></fui-tag>
								<fui-tag text="#2024早春潮鞋" color="#00188F" :isBorder="false" theme="plain" style="margin-left: 10rpx;"></fui-tag>
							</view>
						</view>
						
					</view>
					<view style="display: flex;flex-wrap: wrap;justify-content: flex-start;align-items: center;">
						<view v-for="(work,index) in worksList" style="display: flex;justify-content: center;align-items: center;background: #F0F0F0;border-radius: 14px;margin-left: 40rpx;margin-top: 40rpx;">
							<view style="width: 160rpx;height: 160rpx;">
								<image :src="work.formatImages[0]" style="width: 100%;height: 100%;border-radius: 28rpx;"></image>
							</view>
							<view style="margin-left: 26rpx;margin-right: 60rpx;">
								<view style="font-size: 18px;font-weight: 500;color: #3D3D3D;">{{work.name}}</view>
								<view style="display: flex;justify-content: space-between;align-items: center;font-size: 12px;color: #666666;margin-top: 8rpx;">
									<view>点赞:{{work.loveNum}}</view>
									<view>被同款:{{work.sameProductNum||0}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view style="display: flex;justify-content: space-between;align-items: center;margin-top: 60rpx;margin-bottom: 60rpx;">
					<view style="width: 30%;cursor: pointer;" @click="showWorks=true">
						<view style="display: flex;flex-direction: column;justify-content:space-between;align-items: center;">
							<image src="/static/image/publish/ft.png" style="width: 70rpx;" mode="widthFix"></image>
							<view style="color: #666666;font-size: 28rpx;">关联创作</view>
						</view>
					</view>
					
					<view style="width: 70%;">
						<view @click="submit" style="cursor: pointer; background: #002BFF;border-radius: 100px;font-size: 36rpx;font-weight: bold;color: #fff;text-align: center;height: 100rpx;display: flex;justify-content: center;align-items: center;">
							<view>发帖</view>
						</view>
					</view>
				</view>
			</view>
		</menu-wrapper>
		<fui-landscape :show="show" :closable="closable" @close="closePopup">
			<view class="fui-ani__box"
				style="background-color: #fff;border-radius: 28rpx;width: 700rpx;height: 430rpx;">
				<view class="flex-center"
					style="height:120rpx;display: flex;justify-content: center;align-items: center;padding: 40rpx 40rpx 0 40rpx;">
					<view style="color: #000000;font-weight: 500;font-size: 40rpx;">帖子已提交审核</view>
				</view>
				<view class="flex-center"
					style="height:100rpx;display: flex;justify-content: center;align-items: center;padding: 0 40rpx 0 40rpx;">
					<view style="color: #666666;font-weight: 500;font-size: 32rpx;">审核结果可移步通知页查看</view>
				</view>
				<view style="padding:50rpx;display: flex;justify-content: space-between;align-items: center;">
					<view @click="show=false"
						style="cursor: pointer; background: #0033FF;border-radius: 200rpx;font-size: 36rpx;font-weight: bold;color: #fff;padding: 15rpx 90rpx 15rpx 90rpx;width: 100%;text-align: center;">
						知道了
					</view>
				</view>
			</view>
		</fui-landscape>
		<fui-landscape :show="showImg" :closable="closable1" @close="closePopup1">
			<view class="fui-ani__box" style="background-color: #fff;border-radius: 28rpx;width: 1400rpx;;">
				<view class="flex-center"
					style="height:120rpx;display: flex;justify-content: space-between;align-items: center;padding: 0 40rpx 0 40rpx;">
					<view style="color: #000000;font-weight: bold;font-size: 48rpx;">添加作品图</view>
					<image src="/static/image/user/content/close.png" style="width:80rpx;" mode="widthFix"
						@click="closePopup1">
					</image>
				</view>
				<view style="margin:80rpx;width: 90%;padding-top: 0;">
					<view
						style="display: flex;justify-content: space-between;align-items: center;border-bottom: 1px solid #D8D8D8;">
						<scroll-view scroll-y @scrolltolower="getList">
							<view style="display: flex;flex-wrap: wrap;margin-bottom: 90rpx;align-items: flex-start;">
								<fui-checkbox-group name="checkbox" v-model="vals1" @change="change"
									style="width: 100%;display: flex;flex-wrap: wrap;cursor: pointer;">
									<view v-for="(item,index) in list" v-if="!imageList.includes(item.path)"
										style="position: relative;margin-top: 34rpx;width: 25%;">
										<view style="position: absolute;right: 15%;top: 20rpx;z-index: 1;">
											<fui-checkbox :value="item.path"></fui-checkbox>
										</view>
										<image :src="item.path" style="width: 90%;border-radius: 28rpx;"
											mode="widthFix">
										</image>
									</view>
								</fui-checkbox-group>
							</view>
						</scroll-view>
					</view>
					<view style="display: flex;justify-content: center;align-items: center;margin-top: 50rpx;">
						<view @click="addImg"
							style="font-size: 18px;font-weight: bold;color: #FFFFFF;background: #0033FF;border-radius: 100px;height: 100rpx;width: 100%;display: flex;justify-content: center;align-items: center;cursor: pointer;">
							<view>确认添加</view>
						</view>
					</view>
				</view>
			</view>
		</fui-landscape>
		<fui-landscape :show="showWorks" :closable="closable2" @close="closePopup2">
			<view class="fui-ani__box" style="background-color: #fff;border-radius: 28rpx;width: 1400rpx;;">
				<view class="flex-center"
					style="height:120rpx;display: flex;justify-content: space-between;align-items: center;padding: 0 40rpx 0 40rpx;">
					<view style="color: #000000;font-weight: bold;font-size: 48rpx;">添加关联作品</view>
					<image src="/static/image/user/content/close.png" style="width:80rpx;" mode="widthFix"
						@click="closePopup2">
					</image>
				</view>
				<view style="margin:80rpx;width: 90%;padding-top: 0;">
					<view
						style="display: flex;justify-content: space-between;align-items: center;border-bottom: 1px solid #D8D8D8;">
						<scroll-view scroll-y @scrolltolower="getList">
							<view style="display: flex;flex-wrap: wrap;margin-bottom: 90rpx;align-items: flex-start;">
								<fui-checkbox-group name="checkbox" v-model="vals2" @change="change"
									style="width: 100%;display: flex;flex-wrap: wrap;cursor: pointer;">
									<view v-for="(item,index) in data"
										style="position: relative;margin-top: 34rpx;width: 25%;">
										<view style="position: absolute;right: 15%;top: 20rpx;z-index: 1;">
											<fui-checkbox :value="index"></fui-checkbox>
										</view>
										<image :src="item.formatImages[0]" style="width: 90%;border-radius: 28rpx;"
											mode="widthFix">
										</image>
									</view>
								</fui-checkbox-group>
							</view>
						</scroll-view>
					</view>
					<view style="display: flex;justify-content: center;align-items: center;margin-top: 50rpx;">
						<view @click="addWorks"
							style="font-size: 18px;font-weight: bold;color: #FFFFFF;background: #0033FF;border-radius: 100px;height: 100rpx;width: 100%;display: flex;justify-content: center;align-items: center;cursor: pointer;">
							<view>确认添加</view>
						</view>
					</view>
				</view>
			</view>
		</fui-landscape>

	</view>
</template>

<script>
	import menuWrapper from '@/components/user/common/menu-wrapper.vue';
	import centerConfig from '@/common/config/user/center.config.js';

	import {
		mapState
	} from 'vuex';

	export default {
		components: {
			menuWrapper
		},
		data() {
			return {
				showImg: false,
				closable1: false,
				showWorks: false,
				closable2: false,
				ispl: false,
				...centerConfig,
				accountInfo: {},
				counts: [],
				tabIndex: 0,
				data: [],
				page: 0,
				data1: [],
				page1: 0,
				nameKey: '',
				vals: [],
				show: false,
				closable: false,
				uploadUrl: '',
				attachment: [],
				status: '',
				list:[],
				list1:[],
				vals1: [],
				vals2: [],
				worksList:[],
				imageList:[],
				content:'',
				items:[
					{
						content:'',
						imagelist:[],
					}
				],
				name: "帖子",
				status: 1,
				label: "",
				visible: 1,
				author: "",
			}
		},
		computed: {
			...mapState(['loginUser']),
			currentTime() {
				const currentHour = new Date().getHours();
				const timeQuantums = ['凌晨', '上午', '下午', '晚上'];
				return timeQuantums[Math.floor(currentHour / 6)];
			}
		},
		onLoad(op) {
			this.uploadUrl = getApp().globalData.apiUrl1 + "/api/v1/file/upload";
			// this.tabIndex = op.tabIndex;
			this.getList();
			this.getList1();
			this.getPictureList();
			this.$store.commit('tab', 0);
		},
		onReachBottom() {
			console.log('下拉加载')
			this.getList()
		},
		methods: {
			submit(){
				for (var i = 0; i < this.items.length; i++) {
					this.publish(this.items[i])
				}
				// uni.showToast({
				// 	title: "发表成功",
				// 	icon:'none'
				// })
				
			},
			publish(data) {
				const that = this;
				if (!data.imagelist) {
					uni.showToast({
						title: "请选择图片",
						icon:'none'
					})
					return;
				}
				if (!data.content) {
					uni.showToast({
						title: "请选择填写分享内容",
						icon:'none'
					})
					return;
				}
				this.$util.request('/api/v1/pictureCollection', {
					name: this.name,
					content: data.content,
					status: that.status,
					label: this.label,
					formatImages: data.imagelist,
					type: 3,
					visible: this.visible,
					jobId: 0,
				}, (res) => {
					console.log('myphoto', res)
					that.show=true;
				});
			},
			clear(index){
				this.items[index].content=''
			},
			addItem(){
				this.items.push({
					content:'',
					imagelist:[]
				})
			},
			addImg(){
				this.showImg=false;
				for (var i = 0; i < this.vals1.length; i++) {
					this.items[this.tabIndex].imagelist.push(this.vals1[i])
				}
				this.vals1=[];
			},
			addWorks(){
				this.showWorks=false;
				this.worksList=[];
				for (var i = 0; i < this.vals2.length; i++) {
					this.worksList.push(this.data[this.vals2[i]])
				}
			},
			getPictureList() {
				let that = this;
				var cmd = '';
				if (this.nameKey) {
					cmd = this.nameKey
				}
				this.$util.request('/api/v1/myphoto/mine', {
					page: that.page,
					cmd: cmd
				}, (res) => {
					console.log('myphoto', res)
					if (res.data.length > 0) {
						that.page = that.page + 1;
					}
					that.list = that.list.concat(res.data)
				}, true);
			},
			//上传成功时触发
			success(e) {
				console.log(e)
				//e.res 为服务器返回数据
				//上传成功回调，处理服务器返回数据【此处根据实际返回数据进行处理】
				let res = JSON.parse(e.res.data.replace(/\ufeff/g, "") || "{}")
				this.status = e.status
				console.log(res.data)
				if (res.data) {
					//处理结果返回给组件 
					//res.data.url为上传成功后返回的图片地址
					//e.index为图片索引值
					this.$refs.upload.result(res.data, e.index)
				}
			},
			//上传失败时触发
			error(e) {
				this.status = e.status
			},
			//选择图片或上传完成时触发
			complete(e) {
				this.attachment = e.urls
				if (this.status === 'success') {
					console.log(this.attachment)
				}
			},
			showQx() {
				if (this.vals.length > 0) {
					this.show = true
				} else {
					uni.showToast({
						title: '请选择帖子',
						icon: 'none'
					})
				}
			},
			closePopup2() {
				this.showWorks = false
			},
			closePopup1() {
				this.showImg = false
			},
			closePopup() {
				this.show = false
			},
			change(e) {
				console.log(e)
			},
			goUrl(url) {
				uni.navigateTo({
					url: url,
					fail(err) {
						console.log(err)
					}
				})
			},
			clickOption(index) {
				if (this.vals.length <= 0) {
					uni.showToast({
						title: '请选择作品',
						icon: 'none'
					})
					return false;
				}
				uni.setStorage({
					data: this.vals,
					key: 'picture'
				})
				this.goUrl('/pages/publish/picture')
			},
			getList() {
				let that = this;
				this.$util.request('/api/v1/pictureCollection/mine', {
					page: that.page,
					orderType: 2,
					type: 1
				}, (res) => {
					console.log('pictureCollection', res.data)
					if (res.data.length > 0) {
						that.page = that.page + 1;
					}
					that.data = that.data.concat(res.data);
				}, true);
			},
			
			deletePost() {
				let that = this;
				for (var i = 0; i < this.vals.length; i++) {
					// this.vals[i]
					this.$util.request('/api/v1/pictureCollection/delete/' + this.vals[i], {}, (res) => {
						console.log('pictureCollection', res.data)
					}, true);
				}
				this.getList();
				this.show = false
				this.ispl = false
			},
		}
	}
</script>

<style lang="scss">
	.publish {
		background: #FFFFFF;
		height: 70rpx;
		color: #000000;
		border-radius: 100rpx;
		font-size: 28rpx;
		padding: 0rpx 30rpx 0rpx 30rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border: 1px solid #C4BFBF;
		margin-right: 40rpx;
	}
	.fui-upload__item {
		width: 200rpx;
		height: 200rpx;
		background: rgb(240, 240, 240);
		border-radius: 24rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.wrap {
		display: -webkit-box;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.bottom-item {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-left: 30rpx;

		.image {
			margin-right: 10rpx;
			width: 20rpx;
			height: 20rpx;
		}
	}




	.card-body {
		margin-top: 60rpx;
	}

	.lable {
		font-family: Source Han Sans;
		font-size: 80rpx;
		font-weight: bold;
		font-feature-settings: "kern" on;
		color: #3D3D3D;

	}

	.publish {
		background: #FFFFFF;
		height: 70rpx;
		color: #000000;
		border-radius: 100rpx;
		font-size: 28rpx;
		padding: 0rpx 30rpx 0rpx 30rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border: 1px solid #C4BFBF;
		margin-right: 40rpx;
	}
</style>